package com.myzl.common.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import org.springframework.util.ObjectUtils;

/**
 * 特殊时间获取工具
 * @author cew
 * @date 2023年7月11日17:35:55
 */
public class SpecificDateUtil {

    public static final String ONE_BLANK_SPACE = " ";

    public static final String TRANSVERSE_BAR = "-";

    public static final String YEAR_START_MONTH = "01-01";
    public static final String YEAR_START_DAY = "01";

    public static final String ZERO_TIME = "00:00:00";
    public static final String MS_ZERO_TIME = "00:00:00.000";
    public static final String MS = ".000";

    public static String getCurrentYear(String date){
        if (ObjectUtils.isEmpty(date)){
            LocalDateTimeUtil.now();
            date = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.NORM_DATETIME_PATTERN);
        }
        return DateUtil.year(DateUtil.parse(date)) + SpecificDateUtil.TRANSVERSE_BAR + SpecificDateUtil.YEAR_START_MONTH + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
    }

    public static String getNextYear(String date){
        if (ObjectUtils.isEmpty(date)){
            LocalDateTimeUtil.now();
            date = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.NORM_DATETIME_PATTERN);
        }
        return (DateUtil.year(DateUtil.parse(date))+1) + SpecificDateUtil.TRANSVERSE_BAR + SpecificDateUtil.YEAR_START_MONTH + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
    }

    public static String getPrevYear(String date){
        if (ObjectUtils.isEmpty(date)){
            LocalDateTimeUtil.now();
            date = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.NORM_DATETIME_PATTERN);
        }
        return (DateUtil.year(DateUtil.parse(date))-1) + SpecificDateUtil.TRANSVERSE_BAR + SpecificDateUtil.YEAR_START_MONTH + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
    }

    public static String getCurrentMonth(String date){
        String currentMonthStr = date.substring(0, date.length() - 2) + SpecificDateUtil.YEAR_START_DAY;
        currentMonthStr = currentMonthStr + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
        return currentMonthStr;
    }

    public static String getNextMonth(String date){
        String currentMonthStr = date.substring(0, date.length() - 2) +  SpecificDateUtil.YEAR_START_DAY;
        currentMonthStr = currentMonthStr + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
        DateTime nextMonth = DateUtil.offsetMonth(DateUtil.parse(currentMonthStr), 1);
        return DateUtil.formatDateTime(nextMonth);
    }

    public static String getPrevMonth(String date){
        String currentMonthStr = date.substring(0, date.length() - 2) +  SpecificDateUtil.YEAR_START_DAY;
        currentMonthStr = currentMonthStr + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
        DateTime prevMonth = DateUtil.offsetMonth(DateUtil.parse(currentMonthStr), -1);
        return DateUtil.formatDateTime(prevMonth);
    }

    public static String getCurrentDay(String date){
        return date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
    }

    public static String getNextDay(String date){
        String currentDayStr = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
        DateTime nextDay = DateUtil.offsetDay(DateUtil.parse(currentDayStr), 1);
        return DateUtil.formatDateTime(nextDay);
    }

    public static String getPrevDay(String date){
        String currentDayStr = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.ZERO_TIME;
        DateTime prevDay = DateUtil.offsetDay(DateUtil.parse(currentDayStr), -1);
        return DateUtil.formatDateTime(prevDay);
    }


    public static String getCurrentClassTime(String date,String time){
     return DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(date), -1)) + SpecificDateUtil.ONE_BLANK_SPACE + time.replace("-", "");
    }

    public static String getNextClassTime(String date,String time){
        return date + SpecificDateUtil.ONE_BLANK_SPACE + time.replace("-", "");
    }

    public static String getPrevClassTime(String date,String time){
        return DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(date), -2)) + " " + time.replace("-", "");
    }

    public static DateTime getMsCurrentDay(String date){
        String datetime = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.parse(datetime,DatePattern.NORM_DATETIME_MS_PATTERN);
    }

    public static DateTime getMsCurrentYear(String date){
        if (ObjectUtils.isEmpty(date)){
            LocalDateTimeUtil.now();
            date = LocalDateTimeUtil.format(LocalDateTimeUtil.now(), DatePattern.NORM_DATETIME_PATTERN);
        }
        String datetime = DateUtil.year(DateUtil.parse(date)) + SpecificDateUtil.TRANSVERSE_BAR + SpecificDateUtil.YEAR_START_MONTH + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.parse(datetime,DatePattern.NORM_DATETIME_MS_PATTERN);
    }

    public static DateTime getMsCurrentMonth(String date){
        String currentMonthStr = date.substring(0, date.length() - 2) + SpecificDateUtil.YEAR_START_DAY;
        String datetime = currentMonthStr + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.parse(datetime,DatePattern.NORM_DATETIME_MS_PATTERN);
    }

    public static DateTime getMsPrevDay(String date){
        String currentDayStr = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.offsetDay(DateUtil.parse(currentDayStr,DatePattern.NORM_DATETIME_MS_PATTERN), -1);
    }


    public static DateTime getMsCurrentClassTime(String date,String time){
        String datetime =  DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(date), -1)) + SpecificDateUtil.ONE_BLANK_SPACE + time.replace("-", "") + SpecificDateUtil.MS;
        return DateUtil.parse(datetime,DatePattern.NORM_DATETIME_MS_PATTERN);
    }

    public static DateTime getMsNextClassTime(String date,String time){
        String datetime =  date + SpecificDateUtil.ONE_BLANK_SPACE + time.replace("-", "")+ SpecificDateUtil.MS;
        return DateUtil.parse(datetime,DatePattern.NORM_DATETIME_MS_PATTERN);
    }

    public static DateTime getHalfYear(String date){
        String currentDayStr = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.offsetMonth(DateUtil.parse(currentDayStr,DatePattern.NORM_DATETIME_MS_PATTERN), -6);
    }

    public static DateTime getAgoOneYear(String date){
        String currentDayStr = date + SpecificDateUtil.ONE_BLANK_SPACE + SpecificDateUtil.MS_ZERO_TIME;
        return DateUtil.offsetMonth(DateUtil.parse(currentDayStr,DatePattern.NORM_DATETIME_MS_PATTERN), -13);
    }

}
